<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2021/3/25
  Time: 18:14
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>layout 后台大布局 - Layui</title>
    <link rel="stylesheet" href=${pageContext.request.contextPath}/layui/css/layui.css >

</head>
<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin">
    <jsp:include page="../common/header.jsp"></jsp:include>
    <jsp:include page="../common/menu.jsp"></jsp:include>
    <div class="layui-body">
        <!-- 内容主体区域 -->
        <div style="padding: 15px;">
            <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
                <legend>人员列表</legend>
            </fieldset>
            <div class="layui-form" action="POST">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">输入框</label>
                        <div class="layui-inline">
                            <input type="text" name="firstName" required  lay-verify="required" placeholder="请输入你要查询的姓名" autocomplete="off" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">开始时间</label>
                        <div class="layui-inline">
                            <input name="start" required type="text" class="layui-input" id="start">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">结束时间</label>
                        <div class="layui-inline">
                            <input name="end" required type="text" class="layui-input" id="end">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-input-inline">
                            <button class="layui-btn" lay-submit lay-filter="formEmp">查询</button>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div>
                </div>

                </form>
                <div>
                    <table class="layui-hide" id="test" lay-filter="test"></table>

                    <script type="text/html" id="toolbarDemo">
                        <div class="layui-btn-container">
                            <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
                            <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
                            <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
                            <button class="layui-btn layui-btn-sm" lay-event="addEmp">添加用户信息</button>
                        </div>
                    </script>
                    <script type="text/html" id="barDemo">
                        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                    </script>
                </div>
            </div>
        </div>

        <div class="layui-footer">
            <!-- 底部固定区域 -->
            © layui.com - 底部固定区域
        </div>
    </div>
        <div style="padding: 50px;display: none" id="addEmpDiv">
            <form class="layui-form" action="" lay-filter="empForm" id="addupdateForm">
                <div class="layui-form-item">
                    <label class="layui-form-label">姓:</label>
                    <div class="layui-input-block">
<%--                        隐藏域id，便于修改数据--%>
                        <input type="hidden" name="id" >
                        <input type="text" name="firstName" required  lay-verify="required" placeholder="请输入姓" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">名字:</label>
                    <div class="layui-input-block">
                        <input type="text" name="lastName" required  lay-verify="required" placeholder="请输入名字" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">性别:</label>
                    <div class="layui-input-block">
                        <input type="radio" name="gender" value="男" title="男">
                        <input type="radio" name="gender" value="女" title="女" checked>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">年龄:</label>
                    <div class="layui-input-block">
                        <input type="text" name="age" required  lay-verify="required" placeholder="请输入年龄" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">工资:</label>
                    <div class="layui-input-block">
                        <input type="text" name="salary" required  lay-verify="required" placeholder="请输入工资" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">地址:</label>
                    <div class="layui-input-block">
                        <input type="text" name="address" required  lay-verify="required" placeholder="请输入地址" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">部门:</label>
                    <div class="layui-input-block">
                        <select id="deptList" name="deptId" lay-verify="required">
                            <option value=""></option>
                            <option value="1">测试</option>
                            <option value="2">开发</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">时间:</label>
                    <div class="layui-input-block">
                        <input type="text" name="date" id="date" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="addEmp">立即提交</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>
        </div>
    <script src="${pageContext.request.contextPath}/layui/layui.js"></script>
    <script>
        var $=""
        layui.use(['form','table','laydate','jquery'], function(){
            // 时间控件
            var laydate = layui.laydate;

            var table = layui.table;
            var form=layui.form;
             $=layui.$;
             var index='';

            //温馨提示：默认由前端自动合计当前行数据。从 layui 2.5.6 开始： 若接口直接返回了合计行数据，则优先读取接口合计行数据。
            //详见：https://www.layui.com/doc/modules/table.html#totalRow
            table.render({
                elem: '#test',//id,
                method:'post',
                contentType:'application/json'
                ,url:'emplist'//查询接口
                ,toolbar: '#toolbarDemo'
                ,title: '用户数据表'
                ,totalRow: true
                ,cols: [[
                    {type: 'checkbox', fixed: 'left'}
                    ,{field:'id', title:'ID', width:80, fixed: 'left', unresize: true, sort: true, totalRowText: '合计'}
                    ,{field:'firstName', title:'姓', width:120, edit: 'text'}
                    ,{field:'lastName', title:'名字', width:120, edit: 'text'}
                    ,{field:'gender', title:'性别', width:150, edit: 'text', templet: function(res){
                            return res.gender == '男'?"男" : "女";
                        }}
                    ,{field:'age', title:'年龄', width:120, edit: 'text'}
                    ,{field:'salary', title:'工资', width:120, edit: 'text'}
                    ,{field:'address', title:'地址', width:120, edit: 'text'}
                    ,{field:'deptId', title:'部门ID', width:120, edit: 'text'}
                    ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
                ]]
                ,page: true,
                limit:1,
                limits:[5,10,15,20,25]
            });

            //工具栏事件
            table.on('toolbar(test)', function(obj){
                var checkStatus = table.checkStatus(obj.config.id);
                switch(obj.event){
                    case 'getCheckData':
                        var data = checkStatus.data;
                        layer.alert(JSON.stringify(data));
                        break;
                    case 'getCheckLength':
                        var data = checkStatus.data;
                        layer.msg('选中了：'+ data.length + ' 个');
                        break;
                    case 'isAll':
                        layer.msg(checkStatus.isAll ? '全选': '未全选')
                        break;
                    case 'addEmp':
                        getDeptList();
                        index=layer.open({
                            type: 1,
                            title: ['添加人员','font-size:18px;'],
                            area : ['900px' , '660px'],
                            shadeClose: true, //点击遮罩关闭层
                            content: $('#addEmpDiv'),
                            end: function(){
                                $("#addupdateForm")[0].reset();
                                layui.form.render();
                            }
                        });
                        break;
                };
            });
            //工具栏事件
            table.on('tool(test)', function(obj){
                var data=obj.data;
                var layEvent=obj.event;
                var tr=obj.tr;
               if(layEvent==='del'){

                   var id  = data.id;
                   index=layer.confirm('真的删除吗',function(index){

                       $.ajax({
                           url:"${pageContext.request.contextPath}/employee/del/"+id,
                           dataType:'text',
                           type:'get',
                           async:false,
                           success:function(data){
                               if(data=='true'){
                                   layer.msg("删除成功");
                                   layer.close(index);
                                   //table.reload('test');
                                   table.reload('test');
                               }
                           }
                       })
                   })
                   //table.reload('test');
                   //layer.close(index);
               }else if(layEvent==='edit'){
                   // getDeptList();
                   index=layer.open({
                       type: 1,
                       title: ['修改人员','font-size:18px;'],
                       area : ['900px' , '660px'],
                       shadeClose: true, //点击遮罩关闭层
                       content: $('#addEmpDiv'),
                       end: function(){
                           $("#addupdateForm")[0].reset();
                           layui.form.render();
                       },
                       success:function (layero,index) {
                           $.ajax({
                               url:'${pageContext.request.contextPath}/dept/getList',
                               dataType:'JSON',
                               type:'get',
                               async:false,
                               success:function(d){
                                   $('#deptList').html("<option value='0'>请选择</option>");
                                   $.each(d,function(index,item){
                                       var option=new Option(item.deptName,item.id);
                                       //alert(item.id+"----"+data.deptId);
                                       if(item.id==data.deptId){
                                           option.setAttribute("selected",'true');
                                       }
                                       $('#deptList').append(option);
                                   })
                                   layui.form.render("select");
                               }
                           })
                           form.val("empForm",{
                               "firstName":data.firstName,
                               "lastName":data.lastName,
                               "gender":data.gender,
                               "age":data.age,
                               "salary":data.salary,
                               "address":data.address,
                               "date":data.date,
                               "deptId":data.deptId,
                               "id":data.id
                           })
                       }
                   });
               }
            });
            //执行一个laydate实例
            laydate.render({
                elem: '#start' //指定元素
            });
            laydate.render({
                elem: '#end' //指定元素
            });
            laydate.render({
                elem: '#date' //指定元素
            });
            form.on('submit(formEmp)',function (data) {
                table.reload('test',{
                    where:{
                        firstName:data.field.firstName,
                        lastName:data.field.lastName,
                        start:data.field.start,
                        end:data.field.end,
                        aaa:123
                    }
                });
                return false
            });
            //监听提交
            form.on('submit(addEmp)', function(data){
                if(data.field.id!=''){
                    $.ajax({
                        url:'${pageContext.request.contextPath}/employee/update',
                        data:JSON.stringify(data.field),
                        dataType:'text',
                        contentType:'application/json',
                        type:'post',
                        success:function(data){
                            if(data=='true'){
                                layer.msg("修改成功");
                                layer.close(index);
                                table.reload('test');
                            }
                        }
                    })
                }else{
                    $.ajax({
                        url:'${pageContext.request.contextPath}/employee/insert',
                        data:JSON.stringify(data.field),
                        dataType:'text',
                        contentType:'application/json',
                        type:'post',
                        success:function(data){
                            if(data=='true'){
                                layer.msg("添加成功");
                                layer.close(index);
                                table.reload('test');
                            }
                        }
                    })
                }
                return false;
            });
        });
        function getDeptList() {
            $.ajax({
                url: '${pageContext.request.contextPath}/dept/getList',
                dataType: 'text',
                contentType: 'javascript/json',
                type: 'get',
                success: function (data) {
                    var dept = JSON.parse(data);
                    var str = '<option value="1">请选择</option>';
                    $.each(dept, function () {
                        str += '<option value="' + this.id + '">' + this.deptName + '</option>'
                    })
                    $('#deptList').html(str);
                    layui.form.render("select");
                }
            })
        }
    </script>
</body>
</html>


